package com.n9.qqonline.sales.service;

import com.n9.qq.android.mock.LoginResult;
import com.n9.qq.android.mock.MockAq;
import com.n9.qq.android.mock.VfCodeHandler;
import com.n9.qq.util.VFCodeForm;
import com.n9.qqonline.sales.entity.Config;
import com.n9.qqonline.sales.filter.GroupMsgFilter;
import com.n9.qqonline.sales.filter.KeyWordsFilter;
import com.n9.qqonline.sales.vo.QqVo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author ice
 * @since 2019/3/11 12:01
 */
public final class MsgService {
    private static final MsgService me = new MsgService();

    private RecvMsgService recvMsgService;

    private MsgService() {
        recvMsgService = new RecvMsgService();
        recvMsgService.addFilter("groupMsgFilter", new GroupMsgFilter());

        Config config = new ConfigService().getConfig();
        if (Objects.nonNull(config)) {
            recvMsgService.addFilter("buyMsgFilter", new KeyWordsFilter(config.getBuyKeyWords()));
        }
//        new Thread(()->{
//            while(true) {
//                ThreadUtils.sleep(60*1000);
//
//                for (int i = mockAqs.size() - 1; i >= 0; i--) {
//                    MockAq mockAq = mockAqs.get(i);
//                    if (!mockAq.isOnline()) {
//
//                    }
//                }
//            }
//        }).start();
    }

    public static MsgService instance() {
        return me;
    }

    private List<MockAq> mockAqs = Collections.synchronizedList(new ArrayList<>());

    public boolean login(long qqUin, String password) {
        int tryLoginTimes = 3;
        for (int i = 0; i < tryLoginTimes; i++) {
            try{
                doLogin(qqUin, password);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }


    private void doLogin(long qqUin, String password) {
        VfCodeHandler imgHandler = VFCodeForm.instance();


        MockAq mockAq = new MockAq(qqUin, password, new DefaultMsgListener(recvMsgService), null);
        mockAq.setOfflineListener((client)->{
            new FormService().log(String.format("%d已掉线，开始重新登陆", mockAq.getQqUin()));
            for (int i = 1; i < 4; i++) {
                LoginResult loginResult = mockAq.reLogin(imgHandler);
                if (Objects.equals(LoginResult.SUCCESS, loginResult)) {
                    new FormService().log(qqUin + "掉线重登成功");
                    return;
                } else {
                    new FormService().log(String.format("%d第%d次掉线重登失败:%s", qqUin, i, loginResult.toString()));
                }
            }
        });
        LoginResult loginResult = mockAq.login(imgHandler);
        if (loginResult.equals(LoginResult.SUCCESS)) {
            mockAqs.add(mockAq);

            FormService formService = new FormService();
            formService.log(qqUin + "登陆成功");

            QqVo vo = new QqVo(qqUin, password);
            vo.setGroupCount(Integer.toString(mockAq.getGroupCount()));
            vo.setNick(mockAq.getNick());
            formService.updateQQInfo(vo);
        } else {
            new FormService().log(qqUin + "登陆失败:\t" + loginResult);
        }
    }



}
